package org.intranet.lectorum;

import org.junit.Test;

import junit.framework.Assert;
import junit.framework.TestCase;

public class IsbnTest extends TestCase {
	@Test
	public void testEmptyIsbn() {
		try {
			new Isbn("");
			Assert.fail("Accepts blank ISBN.");
		}
		catch (IllegalIsbnException e) {
			
		}
	}
	
	@Test
	public void testRealIsbn10() throws IllegalIsbnException {
		new Isbn("0-316-08236-8");
	}

	@Test
	public void testBadChecksumIsbn10() {
		try {
			new Isbn("0-316-08236-9");
			Assert.fail("Accepts bad checksum for 10 digit ISBN.");
		}
		catch (IllegalIsbnException e) {
			
		}
	}

	@Test
	public void testRealSpacesIsbn() throws IllegalIsbnException {
		new Isbn("0 316 08236 8");
	}

	@Test
	public void testRealCompactIsbn() throws IllegalIsbnException {
		new Isbn("0316082368");
	}

	@Test
	public void testShortIsbn(){
		try {
			new Isbn("0-31-08236-8");
			Assert.fail("Accepts bad ISBN.");
		}
		catch (IllegalIsbnException e) {
			
		}
	}

	@Test
	public void testBadCharIsbn () {
		try {
			new Isbn("1-2345-6-S-89-0");
			Assert.fail("Accepts invalid characters.");
		}
		catch (IllegalIsbnException e) {
			
		}
	}

	@Test
	public void testBadCharIsbn13 () {
		try {
			new Isbn("978-0-00-639149-X");
			Assert.fail("Accepts 'X' as check digit in ISBN13.");
		}
		catch (IllegalIsbnException e) {
			
		}
	}

	@Test
	public void testIsbn13() throws IllegalIsbnException {
		new Isbn("978-0-00-639149-4");
	}

	@Test
	public void testBadIsbn13Checksum() {
		try {
			new Isbn("978-0-00-639149-3");
			Assert.fail("Accepts bad ISBN13 checksum");
		}
		catch (IllegalIsbnException e) {
			
		}
	}

	@Test
	public void testChecksumIsbn13() {
		if (!Isbn.checksumIsbn13("9780006391494")) {
			Assert.fail("Function checksumIsbn13 does not accept good checksum for 13 digit ISBN");
		}
		if (Isbn.checksumIsbn13("9780006391493")) {
			Assert.fail("Function checksumIsbn13 accepts bad checksum for 13 digit ISBN");
		}
	}

	@Test
	public void testChecksumIsbn10() {
		if (Isbn.checksumIsbn10("0316082369")) {
			Assert.fail("Function checksumIsbn10 accepts bad checksum for 10 digit ISBN");
		}
	}

	@Test
	public void testZeroChecksumIsbn10() {
		if (!Isbn.checksumIsbn10("1552635430")) {
			Assert.fail("Function checksumIsbn10 fails with zero check digit.");
		}
		if (Isbn.checksumIsbn10("1552635431")) {
			Assert.fail("Function checksumIsbn10 fails expecting zero check digit.");
		}
	}

	@Test
	public void testXChecksumIsbn10() {
		if (!Isbn.checksumIsbn10("047111586X")) {
			Assert.fail("Function checksumIsbn10 fails with 'X' check digit.");
		}
		if (Isbn.checksumIsbn10("0471115860")) {
			Assert.fail("Function checksumIsbn10 fails expecting 'X' check digit.");
		}
	}
}
